package packing;

import geometric3D.BoxShape;
import geometric3D.Point;
import geometric3D.XYRectangle;
import geometric3D.XZRectangle;
import geometric3D.YZRectangle;

public class EmptySpaceBlockPair extends BoxShape{

	EmptySpace emptySpace ;
	Point position;
	Block block;
	int maxX;
	int maxY;
	int maxZ;


	public EmptySpace getEmptySpace() {
		return emptySpace;
	}

	public Point getPosition() {
		return position;
	}

	public Block getBlock() {
		return block;
	}
	public PositionSimpleBlockPair getPairMainBlock(){
		return new PositionSimpleBlockPair(position, block.getMainBlock());

	}
	public PositionSimpleBlockPair getPairNextBlock(){
		if(block.getRelation()==BlockRelation.LEFT){
			Point nextPostion = new Point(position.getX()+block.getMainBlock().getWidth(),
					position.getY(),
					position.getZ());
			return new PositionSimpleBlockPair(nextPostion, block.getNextBlock());


		}else if (block.getRelation()==BlockRelation.INFRONT){
			Point nextPostion = new Point(position.getX(),
					position.getY()+block.getMainBlock().getLength(),
					position.getZ());
			return new PositionSimpleBlockPair(nextPostion, block.getNextBlock());

		}else{
			Point nextPostion = new Point(position.getX(),
					position.getY(),
					position.getZ()+block.getMainBlock().getHeight());
			return new PositionSimpleBlockPair(nextPostion, block.getNextBlock());
		}
	}

	public EmptySpaceBlockPair(EmptySpace emptySpace, Block block) {
		super();
		this.emptySpace = emptySpace;
		this.block = block;
		this.position = emptySpace.getPosition();
		if(!block.isSimpleBlock()){
			if(block.getRelation()== BlockRelation.LEFT){
				maxX=  this.position.getX() + block.getMainBlock().getWidth() +block.getNextBlock().getWidth();
			}else{
				int XMain =  this.position.getX()+block.getMainBlock().getWidth();
				int XNext=  this.position.getX()+block.getNextBlock().getWidth();
				maxX =Math.max(XMain, XNext);

			}
			if(block.getRelation()==BlockRelation.INFRONT){
				maxY= this.position.getY() + block.getMainBlock().getLength() + block.getNextBlock().getLength();
			}else{
				int yMain = this.position.getY()+ block.getMainBlock().getLength();
				int yNext = this.position.getY()+ block.getNextBlock().getLength();
				maxY=Math.max(yMain, yNext);

			}

			if(block.getRelation()==BlockRelation.BELOW){
				maxZ=  this.position.getZ() + block.getMainBlock().getHeight()+ block.getNextBlock().getHeight();
			}else{
				int zMain  =this.position.getZ()+ block.getMainBlock().getHeight();
				int zNext  =this.position.getZ()+ block.getNextBlock().getHeight();
				maxZ=Math.max(zMain, zNext);

			}

			this.width = maxX - position.getX();
			this.length = maxY - position.getY();
			this.height = maxZ- position.getZ();
		}else{
			this.maxX = this.position.getX()+ block.getMainBlock().getWidth();
			this.maxY = this.position.getY()+ block.getMainBlock().getLength();
			this.maxZ = this.position.getZ()+ block.getMainBlock().getHeight();
			this.width = block.getMainBlock().getWidth();
			this.length = block.getMainBlock().getLength();
			this.height =block.getMainBlock().getHeight();

		}
	}

	public int getMinX(){
		return this.position.getX();
	}
	public int getMinY(){
		return this.position.getY();
	}
	public int getMinZ(){
		return this.position.getZ();
	}




	public int getMaxX() {
		return maxX;
	}

	public int getMaxY() {
		return maxY;
	}

	public int getMaxZ() {
		return maxZ;
	}

	public XZRectangle getXZRectangle (boolean isPositiveFaced){
		if(isPositiveFaced){
			return new XZRectangle(new Point(position.getX(), getMaxY(), position.getZ()),
					this.width, this.height,true );

		}else{
			return new XZRectangle(position,
					this.width, this.height,false );
		}
	}

	public XYRectangle getXYRectangle (boolean isPositiveFaced){
		if(isPositiveFaced){
			return new XYRectangle(new Point(position.getX(),position.getY(), getMaxZ()),
					this.width, this.length,true );

		}else{
			return new XYRectangle(position,
					this.width, this.length,false );
		}
	}

	public YZRectangle getYZRectangle (boolean isPositiveFaced){
		if(isPositiveFaced){
			return new YZRectangle(new Point(getMaxX(),position.getY(), position.getZ()),
					this.length, this.height,true );

		}else{
			return new YZRectangle(position,
					this.length, this.height,false );
		}
	}

	@Override
	public String toString() {
		return "EmptySpaceBlockPair [block=" + block + ",\n emptySpace="
				+ emptySpace + "]";
	}





}
